/*
 * Powered By www.cardmee.net
 */
package com.ns.school.controller.wave;

import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.BookStockDB;
import com.ns.school.po.edu.BorrowBookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.po.sys.User;
import com.ns.school.po.wave.WaveDevicePO;
import com.ns.school.po.wave.WaveStationPO;
import jakarta.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import com.ns.school.common.constant.StaticMsg;
import com.ns.school.controller.base.BaseController;
import com.ns.school.po.wave.WaveRequestDetailPO;
import com.ns.school.common.base.R;
import com.ns.school.service.wave.WaveRequestDetailService;
import com.ns.school.common.exception.MyException;
import com.ns.school.common.config.SecurityUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wt-97
 */
@Slf4j
@Controller
@RequestMapping("request")
public class WaveRequestDetailController extends BaseController {

    @Autowired
    private WaveRequestDetailService waveRequestDetailService;


    @RequestMapping("/list")
    public String list(ModelMap model) {
        return "view/request/list";
    }


    @RequestMapping("/add")
    public String add(Model model, ModelMap modelMap) {
        Map<String, Object> map = new HashMap<>();
        List<WaveDevicePO> deviceList = waveRequestDetailService.queryAllName(map);
        List<User> userList = waveRequestDetailService.selectAllNames(map);
        modelMap.put("device", deviceList);
        modelMap.put("user", userList);
        return "view/request/add";
    }

    @RequestMapping("/edit")
    public String edit(Model model, Integer id, ModelMap modelMap) {
        Map<String, Object> map = new HashMap<>();
        List<WaveDevicePO> deviceList = waveRequestDetailService.queryAllName(map);
        WaveRequestDetailPO waveRequestDetailPO = waveRequestDetailService.selectWaveRequest(id);// 此处需要通过参数id获取到具体的对象
        List<User> userList = waveRequestDetailService.selectAllNames(map);
        waveRequestDetailPO.setEncryptionId(encode(waveRequestDetailPO.getId()));
        modelMap.put("waveRequest", waveRequestDetailPO);
        modelMap.put("device", deviceList);
        modelMap.put("user", userList);
        return "view/request/edit";

    }


    /**
     * 查询列表
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "getWaveRequestList")
    @ResponseBody
    public R<List<?>> list(HttpServletRequest request, WaveRequestDetailPO params) {
        try {
            return waveRequestDetailService.queryPageList(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 添加
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("insertWaveRequestDetail")
    @ResponseBody
    public R<?> insert(HttpServletRequest request, @RequestBody WaveRequestDetailPO params) {
        try {
            Integer userId = SecurityUtil.id();
            params.setCreateBy(userId);
            return waveRequestDetailService.insert(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 更新
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("updateWaveRequestDetail")
    @ResponseBody
    public R<?> update(HttpServletRequest request, @RequestBody WaveRequestDetailPO params) {
        try {
            if (isEmpty(params.getEncryptionId())) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            Integer id = decode(params.getEncryptionId());
            if (id == null) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            params.setId(id);
            Integer userId = SecurityUtil.id();
            params.setUpdateBy(userId);
            return waveRequestDetailService.update(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 删除
     *
     * @param params
     * @return
     */
    @PostMapping("updateDelWaveRequestDetail")
    @ResponseBody
    public R<?> updateDel(WaveRequestDetailPO params) {

        return waveRequestDetailService.updateDel(params);

    }


    //批量删除
    @PostMapping("updateRequestDel")
    @ResponseBody
    public R<?> updateDelAll(WaveRequestDetailPO params, String ids) {

        return waveRequestDetailService.updateDelAll(params, ids);

    }


    //审核
    @PostMapping("requestReview")
    @ResponseBody
    public LayuiResult<WaveRequestDetailPO> auditBorrow(WaveRequestDetailPO waveRequestDetailPO) {
        LayuiResult result = new LayuiResult<>();
        int count = waveRequestDetailService.checkRequest(waveRequestDetailPO);
        if (count > 0) {
            result.setCode(500);
            result.setMsg("该审核记录已处理，请勿重复审核。");
            return result;
        } else {
            // 执行审核操作
            //waveRequestDetailPO.setId(reviewId);
            waveRequestDetailService.auditRequest(result, waveRequestDetailPO.getId());
            return result;
        }
    }


    //审核拒绝
    @PostMapping("requestReviewFail")
    @ResponseBody
    public LayuiResult<WaveRequestDetailPO> auditRequestFail(WaveRequestDetailPO waveRequestDetailPO) {
        LayuiResult result = new LayuiResult<>();
        int count = waveRequestDetailService.checkRequestFail(waveRequestDetailPO.getId());
        if (count > 0) {
            result.setCode(0);
            result.setMsg("审核拒绝");
            return result;
        } else {
            result.setCode(500);
            result.setMsg("审核错误");
            return result;
        }

    }
}

